package com.example.utils;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * /home/weaver/ecology/WEB-INF/lib
 * @author litao
 *
 */
@Slf4j
public class PinyinUtils extends org.apache.commons.lang3.time.DateUtils {
	
	/**
	 * 获取汉字拼音全拼
	 * @param name
	 * @return
	 */
	public static String getFullPinyin(String name) {
	    // 创建格式化对象
	    HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
	    //设置大小写格式
	    outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	    //设置声调格式
	    outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	    // 放置输入结果
	    StringBuilder result = new StringBuilder();
	    // 字符数组
	    char[] charArray = name.toCharArray();
	    // 遍历字符
	    for (char c : charArray) {
	        // 中文会被变成全拼，非中文会被直接拼接在结果字符串中
	        if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
	            String[] pinyinArray = new String[0];
	            try {
	                pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
	            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
	                badHanyuPinyinOutputFormatCombination.printStackTrace();
	            }
	            if (pinyinArray != null) {
	                result.append(pinyinArray[0]);
	            }
	        } else {
	            result.append(c);
	        }
	    }
	    return result.toString();
	}
	/**
	 * 获取汉字拼音首字母
	 * @param name
	 * @return
	 */
	public static String getPinyinInitial(String name) {
	    // 创建格式化对象
	    HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
	    //设置大小写格式
	    outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	    //设置声调格式
	    outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	    // 放置输入结果
	    StringBuilder result = new StringBuilder();
	    // 字符数组
	    char[] charArray = name.toCharArray();
	    // 遍历字符
	    for (char c : charArray) {
	        // 中文会被变成拼音首字母，非中文会被直接拼接在结果字符串中
	        if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
	            String[] pinyinArray = new String[0];
	            try {
	                pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
	            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
	                badHanyuPinyinOutputFormatCombination.printStackTrace();
	            }
	            if (pinyinArray != null) {
	                result.append(pinyinArray[0].charAt(0));
	            }
	        } else {
	            result.append(c);
	        }
	    }
	    return result.toString();
	}
	/**
	 * 获取姓名的 名首字母+姓的全拼
	 * @param name
	 * @return
	 */
	public static String getEmailName(String name) {
		StringBuffer pinyin = new StringBuffer();
		for(int i=1;i<name.length();i++) {
			pinyin.append(PinyinUtils.getPinyinInitial(name.charAt(i)+""));
		}
		pinyin.append(PinyinUtils.getFullPinyin(name.charAt(0)+""));
//		log.info("姓名:{},转换后简拼1:{}",name,pinyin.toString());
		return pinyin.toString();
	}
	/**
	 * 获取姓名的拼音全拼(如果getEmailName取到的名称已经存在了，获取全拼)
	 * @param name
	 * @return
	 */
	public static String getEmailNameFull(String name) {
		StringBuffer pinyin = new StringBuffer();
		for(int i=0;i<name.length();i++) {
			pinyin.append(PinyinUtils.getFullPinyin(name.charAt(i)+""));
		}
		log.info("姓名:{},转换后全拼:{}",name,pinyin.toString());
		return pinyin.toString();
	}
}
